package hotel.controller.room;

import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Callback;

import java.util.List;

import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;

import hotel.dao.HotelDao;
import hotel.modal.RoomChange;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableView;

import javafx.scene.control.ChoiceBox;

import javafx.scene.control.TableColumn;

public class RentChangeManageController {
	@FXML
	private TableView<RoomChange> tableView;
	@FXML
	private TableColumn<RoomChange, Integer> ChangeIdCol;
	@FXML
	private TableColumn<RoomChange, String> preOrderIdCol;
	@FXML
	private TableColumn<RoomChange, String> preRoomNoCol;
	@FXML
	private TableColumn<RoomChange, String> nowRoomNoCol;
	@FXML
	private TableColumn<RoomChange, String> changeTimeCol;
	@FXML
	private TableColumn<RoomChange, String> CommentCol;
	@FXML
	private Button btnTrailerPage;
	@FXML
	private Button btnHomePage;
	@FXML
	private ChoiceBox<String> condCh;
	@FXML
	private TextField condField;
	@FXML
	private Button btnQuery;
	@FXML
	private Button btnPrevPage;
	@FXML
	private Button btnNextPage;
	@FXML
	private TextField pageField;
	@FXML
	private Label pageCountLabel;
	
	Pager pager = null;
	boolean condSearchMode = false;
	String col = null;
	String cond = null;
	
	@FXML
    public void initialize() {
		setCellFactory();
		
		loadData();
	}
	
	private void loadData() {
		showList(1);
	}
	
	private void showList(int pageNumber) {
		condSearchMode = false;
		ObservableList<RoomChange> data = tableView.getItems();
		QueryResult qrs = HotelDao.getRoomChangeList(pageNumber, 500);
		List<RoomChange> list = qrs.getList(RoomChange.class); 	// 获取结果集
		pager = qrs.getPager(); 							// 获取分页信息
		showPagerInfo();									// 将分页信息显示
		data.setAll(list);
		tableView.refresh();
	}
	
	private void condSearch(int pageNumber) {
		condSearchMode = true;
		col = condCh.getSelectionModel().getSelectedItem();
		cond = condField.getText();
		ObservableList<RoomChange> data = tableView.getItems();
		QueryResult qrs = HotelDao.getRoomChangeCondSearch(col, cond, pageNumber, 500);
		List<RoomChange> list = qrs.getList(RoomChange.class); // 获取结果集
		pager = qrs.getPager(); 							// 获取分页信息
		showPagerInfo();									// 将分页信息显示
		data.setAll(list);
		tableView.refresh();
	}
	
	// Event Listener on Button[#btnQuery].onAction
	@FXML
	public void onBtnQueryAction(ActionEvent event) {
		condSearch(1);
	}

	// Event Listener on Button[#btnTrailerPage].onAction
	@FXML
	public void onBtnTrailerPageAction(ActionEvent event) {
		if (condSearchMode)
			condSearch(pager.getPageCount());
		else
			showList(pager.getPageCount());
	}

	// Event Listener on Button[#btnPrevPage].onAction
	@FXML
	public void onBtnPrevPageAction(ActionEvent event) {
		int page = pager.getPageNumber() - 1;
		if (condSearchMode)
			condSearch(page);
		else
			showList(page);
	}

	// Event Listener on Button[#btnNextPage].onAction
	@FXML
	public void onBtnNextPageAction(ActionEvent event) {
		int page = pager.getPageNumber() + 1;
		if (condSearchMode)
			condSearch(page);
		else
			showList(page);
	}

	// Event Listener on Button[#btnHomePage].onAction
	@FXML
	public void onBtnHomePageAction(ActionEvent event) {
		if (condSearchMode)
			condSearch(1);
		else
			showList(1);
	}
	
	private void showPagerInfo() {
		if(pager == null) return ; 
		pageCountLabel.setText("共 " + pager.getPageCount() + " 页");
		pageField.setText(String.valueOf(pager.getPageNumber()));
		btnPrevPage.setDisable(pager.getPageNumber() <= 1);
		btnNextPage.setDisable(pager.getPageCount() == pager.getPageNumber());
		btnHomePage.setDisable(pager.getPageNumber() == 1);
		btnTrailerPage.setDisable(pager.getPageCount() == pager.getPageNumber());
	}
	
	private void setCellFactory() {
		// TODO Auto-generated method stub
		preOrderIdCol.setCellFactory(generatedStringTableCell());
		preRoomNoCol.setCellFactory(generatedStringTableCell());
		nowRoomNoCol.setCellFactory(generatedStringTableCell());
		changeTimeCol.setCellFactory(generatedStringTableCell());
		CommentCol.setCellFactory(generatedStringTableCell());
		ChangeIdCol.setCellFactory(generatedIntegerTableCell());
		
		// 监听搜索框enter键按下
		condField.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent ke) {
				if (ke.getCode().equals(KeyCode.ENTER)) {
					condSearch(1);
				}
			}
		});

		// 监听显示页数框Enter键按下
		pageField.setOnKeyPressed(new EventHandler<KeyEvent>() {
			@Override
			public void handle(KeyEvent ke) {
				if (ke.getCode().equals(KeyCode.ENTER)) {
					int page = Integer.valueOf(pageField.getText());
					if (condSearchMode)
						condSearch(page);
					else
						showList(page);
				}
			}
		});
	}
	
	private Callback<TableColumn<RoomChange, String>, TableCell<RoomChange, String>> generatedStringTableCell() {
		return new Callback<TableColumn<RoomChange,String>, TableCell<RoomChange,String>>() {
			@Override
			public TableCell<RoomChange, String> call(TableColumn<RoomChange, String> param) {
				TableCell<RoomChange, String> cell = new TableCell<RoomChange, String>() {
					@Override
					protected void updateItem(String item, boolean empty) {
						super.updateItem(item, empty);
						if (empty == true)
							setText(null);
						else
							setText(item);
						setAlignment(Pos.CENTER);
					}
				};
				return cell;
			}
		};
	}
	
	private Callback<TableColumn<RoomChange, Integer>, TableCell<RoomChange, Integer>> generatedIntegerTableCell() {
		return new Callback<TableColumn<RoomChange, Integer>, TableCell<RoomChange, Integer>>() {
			@Override
			public TableCell<RoomChange, Integer> call(TableColumn<RoomChange, Integer> param) {
				TableCell<RoomChange, Integer> cell = new TableCell<RoomChange, Integer>() {
					@Override
					protected void updateItem(Integer item, boolean empty) {
						super.updateItem(item, empty);
						if (empty == true)
							setText(null);
						else
							setText(String.valueOf(item));
						setAlignment(Pos.CENTER);
					}
				};
				return cell;
			}
		};
	}
}
